<template>
  <div class="post-list">
    <article v-for="post in lists" :key="post._id" class="card mb-3 shadow-sm">
      <div class="card-body">
        <h4><router-link :to="{ name: 'post', params: { id: post._id } }">{{post.title}}</router-link></h4>
        <div class="row my-3 align-items-center">
          <div v-if="post.image" class="col-4" style="text-align: center">
            <img :src="post.image && post.image.url" :alt="post.title" class="rounded-lg w-100 h-100">
          </div>
          <p :class="{'col-8': post.image}" class="text-muted">{{post.excerpt}}</p>
        </div>
        <span class="text-muted">{{post.createdAt}}</span>
      </div>
    </article>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, PropType } from 'vue'
import { PostProps, ImageProps } from './../store'
import { generateFitUrl } from './../helper'
export default defineComponent({
  name: 'post-list',
  props: {
    list: {
      type: Array as PropType<PostProps[]>,
      required: true
    }
  },
  setup (props) {
    const lists = computed(() => {
      return props.list.map((column) => {
        generateFitUrl(column.image as ImageProps, 200, 110, ['m_fill'])
        return column
      })
    })
    return {
      lists
    }
  }
})
</script>

<style scoped>
img {
  width: 100px !important;
  height: 100px !important;
}
.card-body a {
  color: #333;
  text-decoration: none;
}
.card-body a:hover {
  color: #0d6efd;
}
</style>
